package search.entities;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Id;

import com.googlecode.objectify.annotation.Indexed;
import com.googlecode.objectify.annotation.Unindexed;

import entities.Link;

public class LinkSearch {
	@Id 
	private String id;
	
	@Unindexed 
	private String name = "";
	
	@Unindexed 
	private String description = "";
	
	@Unindexed 
	private String url = "";
	
	@Indexed 
	private String creator = "";
	
	@Indexed 
	private String category = "";
	
	@Indexed 
	private long created = new Date().getTime();
	
	@Indexed 
	private int status = 2; //0-Inactive, 1-Pending, 2-Active

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public long getCreated() {
		return created;
	}

	public void setCreated(long created) {
		this.created = created;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}
	
	public static LinkSearch convert(Link obj) {
		LinkSearch objSearch = new LinkSearch();
		for (Field f : objSearch.getClass().getDeclaredFields()) {
			f.setAccessible(true);
			try {

				Field fobj = obj.getClass().getDeclaredField(f.getName());
				fobj.setAccessible(true);

				f.set(objSearch, fobj.get(obj));

			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		return objSearch;
	}

	public static Link reConvert(LinkSearch objSearch) {
		Link obj = new Link();

		for (Field f : obj.getClass().getDeclaredFields()) {
			f.setAccessible(true);
			try {

				Field fobj = objSearch.getClass().getDeclaredField(f.getName());
				fobj.setAccessible(true);

				f.set(obj, fobj.get(objSearch));

			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		return obj;
	}

	public static List<Link> reConvertList(List<LinkSearch> objsSearch) {
		List<Link> objs = new ArrayList<Link>();
		Link obj = new Link();

		for (LinkSearch objSearch : objsSearch) {
			if (objSearch != null) {
				obj = reConvert(objSearch);
				if (obj != null)
					objs.add(obj);
			}
		}

		return objs;
	}
}
